package com.hzz.dao.user;

import com.hzz.entity.user.User;
import com.hzz.framework.dao.IBaseDao;
import com.hzz.framework.entity.IEntity;
import org.apache.ibatis.annotations.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>组织架构_员工  数据库处理接口类。
 */
@Mapper
public interface IUserDao extends IBaseDao {
    /**
     * 判断是否存在
     */
    @Override
    @Select("select IFNULL(count(0),0) as count from user where  id = #{id} ")
    int isDataExist(IEntity entity) throws Exception;

    /**
     * 逻辑删除
     */
    @Override
    @Update("update user set version=version+1, date_updated=now(), del_flag=1 where  id = #{id} ")
    int deleteById(IEntity entity) throws Exception;

    /**
     * 账号停用
     */
    @Update("update user set version=version+1, date_updated=now(), state=#{state} where  id = #{id} ")
    int disabled(IEntity entity) throws Exception;

    /**
     * 根据主键 物理删除
     */
    @Override
    @Delete("delete from user where  id = #{id} ")
    int deleteByPrimaryKey(IEntity entity) throws Exception;
    /**
     * 恢复逻辑删除的数据
     */
    @Override
    @Update("update user set version=version+1 ,del_flag=0 where  id = #{id} ")
    int recoveryDataById(IEntity dto) throws Exception;

    /**
     * <p>获取员工信息>根据员工登录名。
     */
    User findUserByAccount(Map dto) throws Exception;

    User selectUserPasswordByPrimaryKey(IEntity entity) throws Exception;

    /**
     * <p>判断员工账号是否存在
     */
    @Select(" select count(0) from user where  account=#{account} ")
    int isAccountExist(@Param("account") String account) throws Exception;

    /**
     * <p>密码修改
     */
    @Update("update user set version=version+1,date_updated=now(),password=#{confirmPassword} where id = #{id} and password=#{originalPassword}")
    int updatePassword(IEntity entity) throws Exception;

    /**
     * <p>密码重置
     */
    @Update("update user set version=version+1,date_updated=now(),password=#{password} where id = #{id}")
    int resetPassword(IEntity entity) throws Exception;

    /**
     * <p>最后登陆记录
     */
    @Update("update user set count=count+1,last_login=now() where id = #{id}")
    int lastLogin(IEntity entity) throws Exception;

    /**
     * <p>获取用户及用户所在组织。
     */
    List findUserAndOrgDataIsList(IEntity entity) throws Exception;

    List<?> findRiverStaffIsList(IEntity entity) throws Exception;

    @Select("select id from user where  account=#{account} and del_flag = 0 limit 1")
    Object findUserIdByAccount(String account) throws Exception;
    @Update("update `user` set version=version+1,del_flag = 1 where id in ( select a.id from ( select id from user WHERE id > 1000 AND id < 999999999 AND id NOT IN ( SELECT max( id ) as id FROM USER t1 WHERE t1.id > 1000 AND t1.id < 999999999 GROUP BY t1.account)) a ) and del_flag = 0")
    int updateInvalidUser() throws Exception;

    @Delete("delete from user where del_flag = 1")
    int dropInvalidUser() throws Exception;

    @Update("UPDATE `user` , ey_base_retrieve_gm " +
            " SET `user`.id = ey_base_retrieve_gm.id " +
            " WHERE `user`.del_flag = 0 " +
            " AND `user`.account = ey_base_retrieve_gm.phone " +
            " AND `user`.id != ey_base_retrieve_gm.id " +
            " AND (select count(1) from ey_base_retrieve_gm where `user`.account = ey_base_retrieve_gm.phone) = 1 " +
            " AND ey_base_retrieve_gm.pac LIKE '511025%' ")
    int syncGMCoverUId() throws Exception;
}
